<?php

$idata       = $_POST['idata'] ?? '6Dszzu/a8FsXmYfkkC/hi6PI8o8WIRkLvaA3SrV1IivYk+l14j9LTSqAmPPVW5nHeIAF3eC5jdBhclG3PECJGTOwA+xVvgiGbCPFyYynMb6fP1Bna13M8FEXFQ6M8BWoSemlSGcKNTz9itUV9IqUq9R745zTEHQN7Ml7a9VkCPagOhA8YO4pEHRAb3NHuS4Yt0sLPUx5+imdpkWaZSA9uA==';
$iv          = $_POST['iv'] ?? 'POMMrqjx6zpgYJjgqWQHKA==';
$code        = $_POST['code'] ?? 'tuQ6qsvKDOb-p9nbp0tTREoQebIA7DZEz-4lPGUzuJF8mb66QXZmLGMetF3RRJMps2Xn8EAcIbpQQ7dAvbn48NEOx3w_J9Ub_rennJ6pldrK9vMaxRC5TJfcV2Q';
$appid       = ''; 
$appSecret   = '';
$ttapi       = 'https://developer.toutiao.com/api/apps/v2/jscode2session';
$res         = GetBody($ttapi, json_encode(['appid' => $appid, 'secret' => $appSecret, 'code' => $code])); 
$session_key = $res['session_key'] ?? '';
if ($session_key == '') {
    echo 'session有误';exit;
} 
$phoneInfo = openssl_decrypt(base64_decode($idata, true), 'AES-128-CBC', base64_decode($session_key), OPENSSL_RAW_DATA, base64_decode($iv)); 
if (!$phoneInfo) { 
    echo '解析有误';exit;
} else { 
    $phone = json_decode($phoneInfo, true)['phoneNumber']; 
} 

echo $phone;

function GetBody($url, $xml, $method='POST'){    
    $second = 30;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    $data = curl_exec($ch);
    if($data){
      curl_close($ch);
      return json_decode($data, true);
    } else { 
      $error = curl_errno($ch);
      curl_close($ch);
      return false;
    }
}
